How to Export Query Results to a .txt File when using SQLcl (Oracle) |
您所在的位置:网站首页 › oracle select outfile › How to Export Query Results to a .txt File when using SQLcl (Oracle) |
When querying Oracle Database, you can use the SPOOL command to export your query results to a text file when using SQLcl. ExampleHere’s an example that exports a whole table: SPOOL '/Users/barney/data/regions.txt'; SELECT * FROM regions; SPOOL off;Here’s what it did, line by line: The first line uses the SPOOL command to specify where the output file will be written. Be sure to change /Users/barney/data/regions.txt to a location on your system, and an appropriate file name.On the second line, I ran the SQL query – the results for which I’m exporting. In this case, I exported the whole regions table.Next, I turned SPOOL off.Here’s what the resulting file looks like: REGION_ID REGION_NAME ____________ _________________________ 1 Europe 2 Americas 3 Asia 4 Middle East and Africa 4 rows selected.In my case, my SQLFORMAT was set to ansiconsole, and so the output reflects that. You can change this to any format that suits. Here’s an example of exporting the result in a format that can be used as an import file with sql*loader: SET SQLFORMAT loader SPOOL '/Users/barney/data/regions_loader.txt'; SELECT * FROM regions; SPOOL off; SET SQLFORMAT ansiconsoleResult: 1|"Europe"| 2|"Americas"| 3|"Asia"| 4|"Middle East and Africa"| 4 rows selected.In this case I also reset the SQLFORMAT back to ansiconsole once the SPOOL operation had completed. Remove FeedbackYou can remove the X rows selected with SET FEEDBACK off: SET SQLFORMAT ansiconsole SET FEEDBACK off SPOOL '/Users/barney/data/regions_no_feedback.txt'; SELECT * FROM regions; SPOOL off; SET FEEDBACK onResult: REGION_ID REGION_NAME ____________ _________________________ 1 Europe 2 Americas 3 Asia 4 Middle East and AfricaIn this case I turned FEEDBACK back on after exporting the file. Append the ResultsBy default, SPOOL uses REPLACE, which replaces the file if it already exists. However, we can use the APPEND argument to append the results to the file. Example: SET SQLFORMAT ansiconsole SET FEEDBACK off SPOOL '/Users/barney/data/regions_no_feedback.txt' APPEND; SELECT * FROM regions; SPOOL off; SET FEEDBACK onResulting file: REGION_ID REGION_NAME ____________ _________________________ 1 Europe 2 Americas 3 Asia 4 Middle East and Africa REGION_ID REGION_NAME ____________ _________________________ 1 Europe 2 Americas 3 Asia 4 Middle East and AfricaThis example appended the results to the file that was created (and populated) in the previous example. This resulted in the result being duplicated in the file. Overwrite the Existing FileWe can use REPLACE to overwrite the existing file with the results of a new query: SET SQLFORMAT ansiconsole SET FEEDBACK off SPOOL '/Users/barney/data/regions_no_feedback.txt' REPLACE; SELECT * FROM regions; SPOOL off; SET FEEDBACK onResulting file: REGION_ID REGION_NAME ____________ _________________________ 1 Europe 2 Americas 3 Asia 4 Middle East and AfricaAs mentioned, REPLACE is the default setting, so we could have simply omitted this argument altogether. |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |